package com.qfedu.usermybatis.controller;

import com.github.pagehelper.PageInfo;
import com.qfedu.usermybatis.entity.User;
import com.qfedu.usermybatis.service.UserService;
import com.qfedu.usermybatis.service.impl.UserServiceImpl;

import java.io.*;
import java.util.Objects;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        try{
            // 获取传递的参数,查询第几页/每页几条数据
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            if (Objects.isNull(pageNum) || "".equals(pageNum)){
                pageNum="1";
            }
            if (Objects.isNull(pageSize) || "".equals(pageSize)){
                pageSize="5";
            }
            PageInfo<User> pageInfo  = userService.selectAll(Integer.valueOf(pageNum), Integer.valueOf(pageSize));

            req.setAttribute("pageInfo",pageInfo);

            req.getRequestDispatcher("/jsp/showAll.jsp").forward(req,resp);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}